
import Vue from 'vue'
import Vuex from 'vuex'
// vuex持久化
import VuexPersistence from 'vuex-persist'
Vue.use(Vuex)
// 存储到本地浏览器
const vuexLocal = new VuexPersistence({
  storage: window.localStorage
})

export default new Vuex.Store({
  state: {
    menus: JSON.parse(localStorage.getItem('menus')) || [],
    token: ''
  },
  getters: {
    // 处理后的左侧菜单
    leftmenu (state, val) {
      state.leftList = val
      const arr = []
      state.menus.forEach(item => {
        if (item.postion === 'left') {
          item.name = item.name.substring(0, 2)
          arr.push(item)
        }
      })
      return arr
    },
    // 处理后的设置按钮
    topmenu (state, val) {
      state.leftList = val
      const arr = []
      state.menus.forEach(item => {
        if (item.postion === 'top') {
          item.name = item.name.substring(0, 6)
          arr.push(item)
        }
      })
      return arr
    }
  },
  mutations: {
    // 登录获取的数据
    leftList (state, val) {
      state.menus = val
      localStorage.setItem('menus', JSON.stringify(state.menus))
    },
    // 登录获取的Token
    getToken (state, val) {
      state.token = val
      localStorage.setItem('token', JSON.stringify(state.token))
    }
  },
  actions: {
  },
  modules: {
  },
  // 使用vuex持久化
  plugins: [vuexLocal.plugin]
})
